import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable, of} from "rxjs";
import {User} from "./User";
import {catchError} from 'rxjs/operators';

@Injectable({
    providedIn: 'root'
})

export class UserServiceService {
    /**
     * Web API 的 URL
     * @private
     */
    private userUrl = 'http://127.0.0.1:9999/user';

    /**
     *私有化处理
     */
    constructor(private http: HttpClient) {
    }

    /**
     * 请求错误处理
     * @param operation
     * @param result
     * @private
     */
    private handleError<T>(operation = 'operation', result?: T) {
        return (error: any): Observable<T> => {
            console.error("报错了")
            console.error(error); // log to console instead
            // 通过返回空结果让应用程序继续运行.
            return of(result as T);
        };
    }

    /**
     * 请求头参数-----json格式
     */
    httpOptions = {
        headers: new HttpHeaders({'Content-Type': 'application/json'})
    };

    /**
     * GET 从服务器获取全部用户
     */
    getUsers(): Observable<any> {
        return this.http.get(this.userUrl+`/getall`)
            .pipe(
                catchError(this.handleError<User[]>('getUsers', []))
            );
    }

    getUsersPage(current:number,limit:number): Observable<any> {
        return this.http.get(`${this.userUrl}/page/${current}/${limit}`)
            .pipe(
                catchError(this.handleError<User[]>('getUsers', []))
            );
    }


    /**
     * GET  通过 id 获取用户。如果找到 id 将返回 404
     */
    getUser(id: number): Observable<any> {
        const url = `${this.userUrl}/getbyid/${id}`;
        return this.http.get(url)
            .pipe(
                catchError(this.handleError<User>(`getUsers id=${id}`))
            );
    }

    /**
     * POST:向服务器添加新用户
     */
    addUser(User: User): Observable<any> {
        return this.http.post(`${this.userUrl}/save`, User, this.httpOptions).pipe(
            catchError(this.handleError<User>('addUser'))
        );
    }

    /**
     * PUT: 更新服务器上的用户
     */
    updateHero(user: User): Observable<any> {
        console.log(user)
        return this.http.put(`${this.userUrl}/edit`, user, this.httpOptions).pipe(
            catchError(this.handleError<any>('updateUser'))
        );
    }
    // 获取表信息
    getUserTable():Observable<any> {
        return this.http.get(`${this.userUrl}/new/getdata`)
            .pipe(
                catchError(this.handleError<User[]>('getUserTable', []))
            );
    }
    // 表复制
    tableCopy():Observable<any>{
        return this.http.post(`${this.userUrl}/new/copy`,{},this.httpOptions)
        .pipe(
            catchError(this.handleError<User[]>('getUserTable', []))
        );
    }
     /**
     * GET 通过数据表名获取数据，并分页
     */
      getTable(tableName:any,current:number,limit:number): Observable<any> {
        return this.http.get(`${this.userUrl}/new/allvalue/${tableName}/${current}/${limit}`)
            .pipe(
                catchError(this.handleError<User[]>('getTable', []))
            );
    }

    getTableNames(): Observable<any> {
        return this.http.get(`${this.userUrl}/new/alltable`)
            .pipe(
                catchError(this.handleError<User[]>('getTable', []))
            );
    }



      /**
     * POST:向服务器添加新用户
     */
       copy(filedArrays: any): Observable<any> {
        return this.http.post(`${this.userUrl}/new/translate`, filedArrays, this.httpOptions).pipe(
            catchError(this.handleError<User>('addUser'))
        );
    }

}


